package com.luoran.bartalks.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.core.ext.Pageable;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.BtMateriel;
import com.luoran.bartalks.entity.biz.BtMaterielType;
import com.luoran.bartalks.entity.biz.BtPackingList;
import com.luoran.bartalks.entity.biz.BtPackingListMateriel;
import com.luoran.bartalks.entity.biz.BtPlanDetails;
import com.luoran.bartalks.service.IBtCodingRuleService;
import com.luoran.bartalks.service.IBtMaterielService;
import com.luoran.bartalks.service.IBtMaterielTypeService;
import com.luoran.bartalks.service.IBtPackingListMaterielService;
import com.luoran.bartalks.service.IBtPackingListService;
import com.luoran.bartalks.service.IBtPlanDetailsService;
import com.luoran.bartalks.utils.EnumBillType;
import com.luoran.bartalks.utils.EnumScanType;
import com.luoran.bartalks.utils.UUID;

/**
 * 箱单的Controller
 * 
 * @author wmm 2018年10月20日 下午23:30:29
 */
@Controller
@RequestMapping("/packingList")
public class BtPackingListAction implements Pageable {

	@Autowired
	private IBtPackingListService packingListService;

	@Autowired
	private IBtCodingRuleService codingRuleService;

	@Autowired
	private IBtPackingListMaterielService packingListMaterielService;

	@Autowired
	private IBtPlanDetailsService planService;

	@Autowired
	private IBtMaterielService materielService;

	@Autowired
	private IBtMaterielTypeService materielTypeService;

	public static final Logger log = LoggerFactory.getLogger(BtPackingListAction.class);

	@RequestMapping
	public String lookPackingListManage() {
		return "projectPlanManage/addProject";

	}

	// 分页
	@Override
	public PageQuery<? extends BaseInfo> getQueryList(PageQuery<? extends BaseInfo> pageQuery) {

		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>) pageQuery.getParas();
		// 判断查询条件是否含有计划ID
		if (StringUtils.isEmpty(map.get("planId"))) {
			map.put("planId", null);
		} else {
			map.put("planId", map.get("planId"));
		}
		if (StringUtils.isEmpty(pageQuery.getOrderBy())) {
			pageQuery.setOrderBy("create_time desc");
		}
		pageQuery.setPageSize(5);

		return packingListService.queryList(pageQuery);
	}

	// 箱单物料导入模板下载
	@GetMapping("/download")
	public void downloadFile(String fileName, HttpServletResponse res) {
		InputStream fis = this.getClass().getResourceAsStream(fileName);
		if (fis == null) {
			try {
				res.setContentType("text/html;charset=utf-8");
				res.getWriter().write("模板资源不存在,请联系管理员!");
				res.flushBuffer();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
		res.setContentType("application/vnd.ms-excel");
		try {
			res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
		}
		try {
			IOUtils.copy(fis, res.getOutputStream());
			res.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
				}
			}
		}
	}

	/**
	 * 把excel文件导入数据库. 需要先把箱单信息插入箱单表, 再把物料信息插入物料表, 再插入箱单物料关系表
	 */
	@RequestMapping("/importExcel/{id}")
	@ResponseBody
	public JSONObject upXls(MultipartHttpServletRequest request, @PathVariable("id") String id) {// 计划ID
		JSONObject result = new JSONObject();
		for (Iterator<String> it = request.getFileNames(); it.hasNext();) {
			String fname = it.next();
			if (!StringUtils.isEmpty(fname)) {
				MultipartFile mulFile = request.getFile(fname);
				if (mulFile != null) {
					try {
						InputStream is = mulFile.getInputStream();
						try {
							// 得到工作簿对象
							HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
							// 得到工作表对象
							HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
							result = checkExcelTemplate(sheet);// 模板校验
							if (!result.getBooleanValue("flag")) {
								return result;
							}
							// 获取第三行拿到箱单信息.
							HSSFRow thirdRow = sheet.getRow(2);
							// 箱单对象接收excel第三行读取的箱单信息
							BtPackingList packingList = new BtPackingList();
							packingList.setOwner(getStringValueFromCell(thirdRow.getCell(0)));// 设置所有人
							packingList.setShipper(getStringValueFromCell(thirdRow.getCell(1)));// 设置发货人
							packingList.setReceiver(getStringValueFromCell(thirdRow.getCell(2)));// 设置收货人
							packingList.setEmptyWeight(new BigDecimal(getStringValueFromCell(thirdRow.getCell(3))));// 设置净重
							packingList.setGrossWeight(new BigDecimal(getStringValueFromCell(thirdRow.getCell(4))));// 设置毛重
							packingList.setVolume(new BigDecimal(getStringValueFromCell(thirdRow.getCell(5))));// 设置体积
							if ("是".equals(getStringValueFromCell(thirdRow.getCell(6)))) {
								packingList.setTakePhoto(0);// 设置是否强制拍照
							} else {
								packingList.setTakePhoto(1);// 设置是否强制拍照
							}
							// 根据计划ID获取计划信息.
							log.info("根据计划ID获取计划详情>>>>");
							BtPlanDetails planInfo = planService.getItemById(id);
							packingList.setStartPlace(planInfo.getStartPlaceName());// 设置起始地
							packingList.setDestination(planInfo.getDestinationName());// 设置目的地
							log.info("调编码规则接口生成箱单编码JSON数据>>>>>>");
							JSONObject packingListCodeJson = codingRuleService.generateCoding(EnumBillType.PACKINGLIST,
									UserContext.get().getCompanyId());
							log.info("箱单编码JSON数据> [{}]", packingListCodeJson);
							
							if(packingListCodeJson.getBooleanValue("flag")==false) {
								packingList.setPackingListCode("ExcelRead-"+UUID.get());
							}else {
								packingList.setPackingListCode(packingListCodeJson.getString("msg"));// 设置箱单编码
							}
							packingList.setPlanId(id);// 计划ID
							packingList.setCreateTime(new Date());
							packingList.setStatus(0);
							packingList.setCompanyId(UserContext.get().getCompanyId());
							String packingListId = UUID.get();
							packingList.setId(packingListId);
							// 新增箱单.返回箱单ID
							// String packingListId = packingListService.addPackingList(packingList);
							List<BtMateriel> materiels = new ArrayList<BtMateriel>();
							List<BtPackingListMateriel> packingListMateriels = new ArrayList<BtPackingListMateriel>();
							if (packingListId != null) {
								log.info("箱单导入成功<<<");
								result = readExcel(sheet, packingListId, packingListCodeJson, id, materiels,
										packingListMateriels);// 导入箱单中物料信息
								if (!result.getBooleanValue("flag")) {
									return result;
								} else {
									JSONArray materielsJsonArray = result.getJSONArray("materiels");
									materiels = JSONObject.parseArray(materielsJsonArray.toJSONString(),
											BtMateriel.class);
									JSONArray packingListMaterielsJsonArray = result
											.getJSONArray("packingListMateriels");
									packingListMateriels = JSONObject.parseArray(
											packingListMaterielsJsonArray.toJSONString(), BtPackingListMateriel.class);
									packingListService.leadingIn(packingList, materiels, packingListMateriels);
								}
							} else {
								result.put("flag", false);
								result.put("msg", "导入失败请重试!");
								return result;
							}
						} finally {
							is.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
						result.put("flag", false);
						result.put("msg", "导入失败请联系管理员!");
						return result;
					}
				}
			}
		}
		result.put("flag", true);
		result.put("msg", "导入成功!");
		return result;
	}

	// 生成箱单编码
	@RequestMapping("/generateCode")
	@ResponseBody
	public JSONObject getCode() {
		return codingRuleService.generateCoding(EnumBillType.PACKINGLIST, UserContext.get().getCompanyId());
	}

	// 根据计划ID获取计划信息.
	public BtPlanDetails getPlanInfo(String id) {
		return planService.getItemById(id);
	}

	// 手动添加箱单
	@RequestMapping("/addPackingList")
	@ResponseBody
	public JSONObject addPackingList(BtPackingList packingList) {
		JSONObject result = new JSONObject();
		if (StringUtils.isEmpty(packingList.getPlanId())) {
			result.put("flag", false);
			result.put("msg", "请先指定一个计划再进行添加");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getPackingListCode())) {
			result.put("flag", false);
			result.put("msg", "编码不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getOwner())) {
			result.put("flag", false);
			result.put("msg", "所有者不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getShipper())) {
			result.put("flag", false);
			result.put("msg", "发货人不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getReceiver())) {
			result.put("flag", false);
			result.put("msg", "收货人不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getEmptyWeight())) {
			result.put("flag", false);
			result.put("msg", "请输入箱单净重");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getGrossWeight())) {
			result.put("flag", false);
			result.put("msg", "请输入箱单毛重");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getVolume())) {
			result.put("flag", false);
			result.put("msg", "请输入箱单体积");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getTakePhoto())) {
			result.put("flag", false);
			result.put("msg", "请选择是否强制拍照");
			return result;
		}

		// 根据计划ID获取计划信息.
		BtPlanDetails planInfo = planService.getItemById(packingList.getPlanId());

		packingList.setStartPlace(planInfo.getStartPlaceName());// 设置起始地
		packingList.setDestination(planInfo.getDestinationName());// 设置目的地

		packingList.setCreateTime(new Date());
		packingList.setStatus(0);
		packingList.setCompanyId(UserContext.get().getCompanyId());

		// 插入到数据库
		String packingListId = packingListService.addPackingList(packingList);
		if (packingListId == null) {
			result.put("flag", false);
			result.put("msg", "操作失败,请重试!");
			return result;
		} else {
			result.put("flag", true);
			result.put("msg", "手动添加箱单成功,请添加物料");
			return result;
		}

	}

	// 查看
	@RequestMapping("/lookPackingList/{id}")
	@ResponseBody
	public BtPackingList lookPackingList(@PathVariable("id") String id) {
		// 根据主键查询
		return packingListService.lookPackingList(id);
	}

	// 更新
	@RequestMapping("/updatePackinglist")
	@ResponseBody
	public JSONObject updatePackinglist(BtPackingList packingList) {
		JSONObject result = new JSONObject();
		if (StringUtils.isEmpty(packingList.getPackingListCode())) {
			result.put("flag", false);
			result.put("msg", "编码不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getOwner())) {
			result.put("flag", false);
			result.put("msg", "所有者不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getShipper())) {
			result.put("flag", false);
			result.put("msg", "发货人不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getReceiver())) {
			result.put("flag", false);
			result.put("msg", "收货人不能为空");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getEmptyWeight())) {
			result.put("flag", false);
			result.put("msg", "请输入箱单净重");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getGrossWeight())) {
			result.put("flag", false);
			result.put("msg", "请输入箱单毛重");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getVolume())) {
			result.put("flag", false);
			result.put("msg", "请输入箱单体积");
			return result;
		}
		if (StringUtils.isEmpty(packingList.getTakePhoto())) {
			result.put("flag", false);
			result.put("msg", "请选择是否强制拍照");
			return result;
		}

		// 根据主键更新
		int count = packingListService.updatePackingList(packingList);

		if (count == 1) {
			result.put("flag", true);
			result.put("msg", "操作成功!");
			return result;
		} else {
			result.put("flag", false);
			result.put("msg", "操作失败请重试!");
			return result;
		}

	}

	// 删除前需要先查询箱单是否有物料
	@RequestMapping("/searchMateriel/{id}")
	@ResponseBody
	public JSONObject searchMateriel(@PathVariable("id") String packingListId) {
		JSONObject result = new JSONObject();
		List<BtPackingListMateriel> materiels = packingListMaterielService.getMaterielsByPackingListId(packingListId);
		// 查询箱单是否含有物料
		if (materiels.size() == 0) {
			result.put("flag", true);
			result.put("msg", "");
			return result;
		} else {
			result.put("flag", true);
			result.put("msg", "当前箱单包含物料!");
			return result;
		}

	}

	// 删除箱单
	@RequestMapping("/deletePackingList/{id}")
	@ResponseBody
	public JSONObject deletePackingList(@PathVariable("id") String id) {
		JSONObject result = new JSONObject();
		// 逻辑删除,更新状态
		BtPackingList packingList = new BtPackingList();
		packingList.setId(id);
		packingList.setStatus(1);

		int count = packingListService.deletePackingList(packingList);
		if (count == 1) {
			result.put("flag", true);
			result.put("msg", "操作成功!");
			return result;
		} else {
			result.put("flag", false);
			result.put("msg", "操作失败请重试!");
			return result;
		}
	}

	/**
	 * 读取Excel的文本内容
	 * 
	 * @param cell
	 * @return
	 */
	public static String getStringValueFromCell(Cell cell) {
		SimpleDateFormat sFormat = new SimpleDateFormat("MM/dd/yyyy");
		DecimalFormat decimalFormat = new DecimalFormat("#.#");
		String cellValue = "";
		if (cell == null) {
			return cellValue;
		} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
			cellValue = cell.getStringCellValue();
		} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
			if (HSSFDateUtil.isCellDateFormatted(cell)) {
				double d = cell.getNumericCellValue();
				Date date = HSSFDateUtil.getJavaDate(d);
				cellValue = sFormat.format(date);
			} else {
				cellValue = decimalFormat.format((cell.getNumericCellValue()));
			}
		} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
			cellValue = "";
		} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
			cellValue = String.valueOf(cell.getBooleanCellValue());
		} else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
			cellValue = "";
		} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
			cellValue = cell.getCellFormula().toString();
		}
		return cellValue;
	}

	/**
	 * excel模板校验
	 * 
	 * @param sheet
	 * @return
	 */
	private JSONObject checkExcelTemplate(HSSFSheet sheet) {
		JSONObject result = new JSONObject();
		// 得到第二行对象和第五行信息
		HSSFRow packingListInfo = sheet.getRow(1);
		HSSFRow materielInfo = sheet.getRow(4);
		// 判断模板第二行是否正确
		log.info("校验模板第二行>>>>");
		for (int i = 0; i < packingListInfo.getLastCellNum(); i++) {
			HSSFCell hssfCell = packingListInfo.getCell(i);// 获得单元格信息
			if (i == 0) {
				if (!"箱单所有者".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 1) {
				if (!"发货人".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 2) {
				if (!"收件人".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 3) {
				if (!"净重(kg)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 4) {
				if (!"毛重(kg)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 5) {
				if (!"体积(cm³)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}

			if (i == 6) {
				if (!"是否强制拍照".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
		}
		log.info("模板第二行校验完成<<<<");
		// 判断模板第五行是否正确
		log.info("校验模板第五行>>>>");
		for (int i = 0; i < materielInfo.getLastCellNum(); i++) {
			HSSFCell hssfCell = materielInfo.getCell(i);// 获得单元格信息
			if (i == 0) {
				if (!"物料名称".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 1) {
				if (!"物料编码".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 2) {
				if (!"长度(cm)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 3) {
				if (!"宽度(cm)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 4) {
				if (!"高度(cm)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 5) {
				if (!"体积(cm³)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 6) {
				if (!"净重(kg)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 7) {
				if (!"毛重(kg)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 8) {
				if (!"价值(元)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 9) {
				if (!"物料数量(件)".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 10) {
				if (!"是否强制拍照".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 11) {
				if (!"二维码信息".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
			if (i == 12) {
				if (!"所属分类编码".equals(getStringValueFromCell(hssfCell))) {
					result.put("flag", false);
					result.put("msg", "Excel文件与模板不符,请重新下载!");
					return result;
				}
			}
		}
		log.info("模板第五行校验完成<<<<");
		result.put("flag", true);
		return result;
	}

	/**
	 * 读取Excel文件并插入数据库
	 * 
	 * @param sheet
	 * @param packingListId
	 * @param packingListCodeJson
	 * @return
	 */
	private JSONObject readExcel(HSSFSheet sheet, String packingListId, JSONObject packingListCodeJson, String planId,
			List<BtMateriel> materiels, List<BtPackingListMateriel> packingListMateriels) {
		JSONObject result = new JSONObject();
		List<String> materielQrcode = packingListMaterielService
				.checkMaterielQrcodeOnlyByCompanyId(UserContext.get().getCompanyId());
		// 从第六行开始获取 一直到最后一行 得到物料信息.
		log.info("获取Excel文件中物料信息>>>");
		for (int i = 5; i <= sheet.getLastRowNum(); i++) {
			log.info("获取到Excel文件中第" + (i + 1) + "行物料记录信息>>>>");
			// 遍历每一行 得到每一行物料记录信息
			HSSFRow row = sheet.getRow(i);
			log.info("校验文件第" + (i + 1) + "行必填项是否填写>>>");
			// 检测每列物料名称是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(0)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料名称为空");
				return result;
			}
			// 检测每列物料编码是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(1)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料编码为空");
				return result;
			}
			// 检测每列物料长度是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(2)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料长度为空");
				return result;
			}
			// 检测每列物料宽度是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(3)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料宽度为空");
				return result;
			}
			// 检测每列物料高度是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(4)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料高度为空");
				return result;
			}
			// 检测每列物料体积是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(5)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料体积为空");
				return result;
			}
			// 检测每列物料净重是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(6)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料净重为空");
				return result;
			}
			// 检测每列物料毛重是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(7)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料毛重为空");
				return result;
			}
			// 检测每列物料价值是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(8)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料价值为空");
				return result;
			}
			int number = new Integer(1);
			// 检测每列物料数量
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(9)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料数量为空");
				return result;
			} else {
				try {
					number = Integer.valueOf(getStringValueFromCell(row.getCell(9)));
				} catch (NumberFormatException e) {
					result.put("flag", false);
					result.put("msg", "导入出错,第" + (i + 1) + "行物料数量为非数字");
					return result;
				}
				if (number > 1) {
					// 检测每列物料二维码信息是否为空
					if (!StringUtils.isEmpty(getStringValueFromCell(row.getCell(11)))) {
						result.put("flag", false);
						result.put("msg", "导入出错,请将第" + (i + 1) + "行二维码信息留空将采用系统默认生成二维码,如若设置二维码信息请将数量填写为1");
						return result;
					}
				}
			}
			// 检测每列物料是否强制拍照是否为空
			if (StringUtils.isEmpty(getStringValueFromCell(row.getCell(10)))) {
				result.put("flag", false);
				result.put("msg", "导入出错,第" + (i + 1) + "行物料是否强制拍照为空");
				return result;
			}
			if (!StringUtils.isEmpty(getStringValueFromCell(row.getCell(11)))) {
				if (materielQrcode.contains(getStringValueFromCell(row.getCell(11)))) {
					result.put("flag", false);
					result.put("msg", "导入出错,第" + (i + 1) + "行二维码信息" + getStringValueFromCell(row.getCell(11))
							+ "已经存在,请确保二维码在公司中的唯一性");
					return result;
				} else {
					materielQrcode.add(getStringValueFromCell(row.getCell(11)));
				}
			}
			log.info("第" + (i + 1) + "行必填项校验完成<<<");

			BtMateriel materiel = new BtMateriel(); // 物料对象接收物料信息
			materiel.setId(UUID.get());
			materiel.setMaterielName(getStringValueFromCell(row.getCell(0)));// 物料名称
			log.info("校验第" + (i + 1) + "行物料编码是否被占用>>>物料编码[{}]", getStringValueFromCell(row.getCell(1)));

			BtMateriel persistentMateriel = materielService.getMaterielByMaterielCode(// 根据物料编码查询物料
					getStringValueFromCell(row.getCell(1)), UserContext.get().getCompanyId());
			if (persistentMateriel != null) {
				log.info("第" + (i + 1) + "行物料编码已存在,物料编码[{}]", getStringValueFromCell(row.getCell(1)));
				result.put("flag", false);
				result.put("msg", "导入提示!,编码为'" + getStringValueFromCell(row.getCell(1)) + "'的物料已存在,请手动添加");
				return result;
			} else {
				log.info("第" + (i + 1) + "行物料编码未被占用<<<");
			}
			materiel.setMaterielCode(getStringValueFromCell(row.getCell(1)));// 物料编码
			materiel.setLength(new BigDecimal(getStringValueFromCell(row.getCell(2))));// 长
			materiel.setWidth(new BigDecimal(getStringValueFromCell(row.getCell(3))));// 宽
			materiel.setHeight(new BigDecimal(getStringValueFromCell(row.getCell(4))));// 高
			materiel.setVolume(new BigDecimal(getStringValueFromCell(row.getCell(5))));// 体积
			materiel.setEmptyWeight(new BigDecimal(getStringValueFromCell(row.getCell(6))));// 净重
			materiel.setGrossWeight(new BigDecimal(getStringValueFromCell(row.getCell(7))));// 毛重
			materiel.setValue(new BigDecimal(getStringValueFromCell(row.getCell(8))));// 价值

			// 判断是否填写分类编码
			log.info("检测第" + (i + 1) + "行是否填写物料分类编码>>>");
			if (!StringUtils.isEmpty(getStringValueFromCell(row.getCell(12)))) {
				// 填写了物料分类编码, 根据填写的物料分类编码查询对应信息
				log.info("检测物料分类编码对应的物料分类是否存在>>");
				BtMaterielType persistentMaterielType = materielTypeService.getMaterielTypeByTypeCode(
						getStringValueFromCell(row.getCell(12)), UserContext.get().getCompanyId());
				if (persistentMaterielType != null) {
					log.info("物料分类存在<<");
					materiel.setTypeId(persistentMaterielType.getId());// 分类id
					materiel.setTypeCode(persistentMaterielType.getTypeCode());// 分类编码
					materiel.setTypeName(persistentMaterielType.getTypeName());// 分类名称
					materiel.setLongCode(
							persistentMaterielType.getLongCode() + "!" + getStringValueFromCell(row.getCell(1)));// 长编码
				} else {
					log.info("物料分类不存在<<");
				}
			} else {
				log.info("第" + (i + 1) + "行未填写物料分类编码<<<");
			}
			materiel.setCompanyId(UserContext.get().getCompanyId());// 公司ID
			materiel.setCreateTime(new Date());// 时间
			materiel.setStatus(0);// 状态
			materiel.setQrcode(EnumScanType.MATERIEL.getValue() + ";" + materiel.getId());// 二维码

			// 插入物料
			materiels.add(materiel);
			// 根据物料数量确定插入的箱单物料关系记录的条数.
			for (int j = 0; j < Integer.valueOf(getStringValueFromCell(row.getCell(9))); j++) {
				// 箱单物料关系对象
				BtPackingListMateriel packingListMateriel = new BtPackingListMateriel();
				String Id = UUID.get();
				packingListMateriel.setId(Id);
				packingListMateriel.setMaterielId(materiel.getId());// 物料id
				packingListMateriel.setMaterielName(getStringValueFromCell(row.getCell(0)));// 物料名称
				packingListMateriel.setMaterielCode(getStringValueFromCell(row.getCell(1)));// 设置物料编码
				packingListMateriel.setMaterielAmount(1);// 设置物料数量
				if ("是".equals(getStringValueFromCell(row.getCell(10)))) {
					packingListMateriel.setTakePhoto(0);
				} else {
					packingListMateriel.setTakePhoto(1);
				}
				packingListMateriel.setPackingListId(packingListId);// 设置箱单id

				packingListMateriel.setPackingListCode(packingListCodeJson.getString("msg"));// 设置箱单编码
				packingListMateriel.setCompanyId(UserContext.get().getCompanyId());// 公司ID
				packingListMateriel.setCreateTime(new Date());
				if (!StringUtils.isEmpty(getStringValueFromCell(row.getCell(11)))) {
					packingListMateriel.setMaterielQrcode(getStringValueFromCell(row.getCell(11)));// 物料二维码
				} else {
					packingListMateriel.setMaterielQrcode(EnumScanType.MATERIEL.getValue() + ";" + Id);// 物料二维码
				}
				log.info("插入箱单物料关系记录>>>");
				packingListMateriels.add(packingListMateriel);
			}

			log.info("第" + (i + 1) + "行箱单物料记录导入成功<<<");
		}
		result.put("flag", true);
		result.put("materiels", materiels);
		result.put("packingListMateriels", packingListMateriels);
		return result;

	}

}
